import {UserDaoInter} from "../dao";
import {Autowired, Singleton} from '../decorator'
import UserInfo from "../entity/UserInfo";

export class UserServiceImpl {
    static userServiceImpl: UserServiceImpl;

    static getInstance(): UserServiceImpl {
        if (!this.userServiceImpl) {
            this.userServiceImpl = new UserServiceImpl();
        }
        return this.userServiceImpl;
    }

    @Autowired("userDaoImpl")
    @Singleton(true)
    userDaoImpl!: UserDaoInter;

    constructor() {
        console.log("UserServiceImpl对象被创建了...")
    }

    register(username: string, pwd: string): boolean {
        console.log("usersevice...register")

        let userDao = Reflect.getOwnPropertyDescriptor(UserServiceImpl.prototype, "userDaoImpl")!.value;
        let userInfo: UserInfo = userDao.findUser(username, pwd);

        return !!userInfo;
    }

    login(username: string, pwd: string): UserInfo | null {
        console.log("进入service ...Login,username:", username);

        let userDao = Reflect.getOwnPropertyDescriptor(UserServiceImpl.prototype, "userDaoImpl")!.value;
        let userInfo: UserInfo = userDao.findUser(username, pwd);
        return userInfo || null
    }
}
